const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");

//引入mongo数据库地址
const db = require("../node-app/config/keys.js").mongoURI;

//连接mongoDB数据库
// console.log(db);
mongoose.connect(db).then(() => {
    console.log("MongoDB数据库已连接");
}).catch((err) => {
    console.log(err);
});

// 配置静态资源管理器
app.use(express.static("public"));

//引入路由模块
const users = require("./routes/api/user.js");
const profile = require("./routes/api/profile.js");
const images=require("./routes/api/images.js");
const wifes=require("./routes/api/wife.js");
const crab=require("./routes/api/futureMarket/crab.js");
const messageBoard=require("./routes/api/messageBoard/index.js");

//使用body-parser中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//初始化passport
app.use(passport.initialize());
require("./config/passport.js")(passport);

//应用级中间件--每次请求都会经过
// app.use((req,res,next)=>{
//     console.log(req.path,req.url,req.route);
//     // next();
// })

//基础路由
app.get("/", (req, res) => {
    res.send("hello world!");
});

//使用routes--路由模块化
app.use("/api/users", users);
app.use("/api/profile", profile);
app.use("/api/images",images);
app.use("/api/wifes",wifes);
app.use("/api/crab",crab);
app.use("/api/messageBoard",messageBoard);

//设置端口号
// console.log(process.env);
const port = process.env.PORT || 5000;

//监听端口服务器
app.listen(port, () => {
    console.log(`静态服务器运行在端口${port}`);
});